using namespace std;

#include<stdio.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdlib>
#include<random_access.hpp>

off_t floatlen;
off_t rows;
off_t cols;
random_access_t * random_access;

void transpose(const char * filename){
  FILE * pFileOut = fopen(filename,"wb");
  for(off_t col=0;col<cols;++col){
    cerr<<"Parsing col "<<col<<endl;
    float invals[rows];
    for(off_t row=0;row<rows;++row){
      invals[row] = random_access->extract_val(row,col);
      //cerr<<"row "<<row<<" col "<<col<<endl;
    }
    char * buffer = reinterpret_cast<char *>(invals);
    fwrite(buffer,floatlen,rows,pFileOut);
  }
  fclose(pFileOut);
  
}

int main(int argc,char * argv[]){
  if(argc<5) {
    cerr<<"Usage: <filename> <input rows> <input cols> <transpose_outfile>\n";
    return 1;
  }
  int arg=0;
  floatlen = sizeof(float);
  const char * filename=argv[++arg];
  rows = atoi(argv[++arg]);
  cols = atoi(argv[++arg]);
  const char * outfile = argv[++arg];
  random_access = new random_access_t(filename,rows,cols);
  transpose(outfile);
  delete random_access;
  //float * vec = new float[cols];
  //access.extract_vec(testrow,cols,vec);
  //cout<<"Vector at row: "<<testrow<<":\n";
  for(int i=0;i<cols;++i){
    //if(i) cout<<" ";
    //cout<<vec[i];
  }
  //cout<<endl;
  //delete[] vec;
  
  
  return 0;
}
